var tb;
var interval;
function getDataByYear(){
	var selectedYear = document.getElementById('utilForm:selectedYear').value;
	$('.year').html(selectedYear);
	var result = eval('('+document.getElementById('utilForm:chartData').value+')');
	if(!$.isEmptyObject(result)){
		initScotterChart(result);
		initPieChart(result);
		initTb(result);
	}else{
		initTb(result);
		$("#scotterChart").html("<div class='box-center'>无数据</div>");
		$("#pieChart").html("<div class='box-center'>无数据</div>");
	}
}
function completeCalculate(){
	window.clearInterval(interval);
	interval = undefined;
	updateBtnForm();
	var selectedYear = document.getElementById('utilForm:selectedYear').value;
	var index = layer.confirm(selectedYear+'年数据更新成功，是否展示新数据？', {
		  btn: ['是','否'] //按钮
		}, function(){
			layer.close(index);
			refreshCurrentData();
			getDataByYear();
			$('#progressDiv').collapse('hide');
		}, function(){
			layer.close(index);
			$('#progressDiv').collapse('hide');
		});
}
function startCalculate(){
	updateBtnForm();
	$('#progressDiv').collapse('show');
	interval = window.setInterval(function() {
		updateProgressForm();
	},10);
}
function getLocalTime(nS) {
	return new Date(nS).toLocaleString();
}
function initPieChart(dataSet) {
	//组织饼图数据([[],[]])
	var minCurrent=0;
	var maxCurrent=200;
	/*for(var i in dataSet){
		//先找极值
		if(dataSet[i].avgCurrent>maxCurrent){
			maxCurrent=dataSet[i].maxCurrent;
		}
	}*/
	var num=10;
	var unit=(maxCurrent-minCurrent)/num;
	var map={};
	var firstCurrent=minCurrent;
	for(var i=0;i<num;i++){
		map["["+firstCurrent+"-"+Number(firstCurrent+unit)+"]"]=0;
		firstCurrent+=unit;
	}
	for(var i in dataSet){
		for(var timeLag in map){
			var startTime=Number(timeLag.split("-")[0].replace("[",""));
			var endTime=Number(timeLag.split("-")[1].replace("]",""));
			if(dataSet[i].avgCurrent>startTime&&dataSet[i].avgCurrent<endTime){
				map[timeLag]++;
			}
		}
	}
	var data=[];
	for(var timeLag in map){
		data.push([timeLag,map[timeLag]]);
	}
	var pie = new Pie("pieChart");
	pie.setPie(data);
	var option = pie.getOption();
	option.title.textStyle.fontSize=16;
	option.title.textStyle.fontWeight='normal';
	option.legend.orient='vertical';
	option.series[0].label.normal.formatter='{d}%';
	pie.setPieOuterRadius('50%');
	pie.setLegendPosition('left','bottom');
	pie.setTitlePosition('center');
	pie.show();
}
function initScotterChart(dataSet) {
	var data=[];
	for(var i in dataSet){
		data.push([dataSet[i].avgCurrent,dataSet[i].ldNum,getLocalTime(dataSet[i].faultTime.time),
		           dataSet[i].faultEquName]);
	}
	var myChart = echarts.init(document.getElementById("scotterChart"));
	option = {
		tooltip : {
			formatter : function(params) {
				var scaData = params.value;
				return "故障时间: " + scaData[2]
						+ '<br/>'  + "线路: "
						+ scaData[3] +  '<br/>'
						+ "平均电流: " + scaData[0]+"kA"+ '<br/>'
				        + "雷击次数: "+scaData[1] + '<br/>'
			}
		},
		polar : {

		},
		angleAxis : {
			type : 'value'
		},
		radiusAxis : {
			axisAngle : 0,
			splitNumber : 6,
			splitArea : {
				show : true
			}
		},
		dataZoom : [ {
			type : 'inside',
			radiusAxisIndex : 0,
			start : 0,
			end : 100
		} ],
		series : [ {
			coordinateSystem : 'polar',
			angleAxisIndex : 0,
			radiusAxisIndex : 0,
			type : 'scatter',
			symbolSize : 6,
			data : data
		} ]
	};
	myChart.setOption(option);
}

function initTb(dataSet){
	var tbData=[];
	for(var i in dataSet){
		tbData.push([getLocalTime(dataSet[i].faultTime.time),dataSet[i].faultEquName,dataSet[i].faultType,dataSet[i].ldNum,dataSet[i].maxCurrent
		             ,dataSet[i].minCurrent,dataSet[i].avgCurrent,dataSet[i].maxStroke,dataSet[i].minStroke,dataSet[i].avgStroke]);
	}
	var myopt = MyTable.getDefaultOptions();
	var column=["故障时间","线路名称","故障类型","雷击次数","最大电流(kA)","最小电流(kA)","平均电流(kA)","最大回击","最小回击","平均回击"];
	myopt.paging = true;
	myopt.searching=true;
	myopt.lengthChange=true;
	if(tb){
		tb.getTable().destroy();
		$("#infoTb").empty();
	}
	tb = new MyTable('infoTb', column, myopt);
	tb.setDataSet(tbData);
	tb.show();
}
